<?php

namespace app\model;

use Qiniu\Auth;
use Qiniu\Config;
use Qiniu\Storage\UploadManager;
use Qiniu\Storage\BucketManager;
use think\facade\Db;
use think\Model;

class Oss extends Model
{

    public static function upload($file):string
    {
//        if(!$file){
//            singleJson(false,'没有文件file');
//        }
//        // 用于签名的公钥和私钥
//        $accessKey = 'nddwxXWjOnfufs65sQybZt3Twv_X5YZbUr6xI5U5';
//        $secretKey = 'QN4LaxyMZbzLizQujUsZ9-DhwtS8pg1GprHAK9_m';
//        $bucket = 'penyou520';
//        // 初始化签权对象
//        $auth = new Auth($accessKey, $secretKey);
//
//        // 生成上传Token
//        $token = $auth->uploadToken($bucket);
//
//        // 构建 UploadManager 对象
//        $uploadMgr = new UploadManager();
//        $fileName = basename($file);
//        $key ='temp/'.$fileName;  //文件名
//        list($ret, $err) = $uploadMgr->putFile($token, $key, $file);
//        echo 2;
//        print_r($ret);
//        print_r($err);
//        echo 3;
//        exit;
//        if ($err !== null) {
//            $array=array('code'=>'1','msg'=>"上传失败".$err,'data'=>[]);
//        } else {
//            $array=array('code'=>'0','msg'=>"上传成功",'data'=>$ret);
//        }
//        echo json_encode($array);
    }

    public  static function move($key= 'storage/uploads/video/20231217/93caac901b8a0f07869bfc584c4205df.mp4',$id){

        if(!$key){
            singleJson(false,'没有文件file');
        }
        if(!$id){
            singleJson(false,'没有id');
        }
        $bucket = 'penyou520';
        $accessKey = 'nddwxXWjOnfufs65sQybZt3Twv_X5YZbUr6xI5U5';
        $secretKey = 'QN4LaxyMZbzLizQujUsZ9-DhwtS8pg1GprHAK9_m';

        $auth = new Auth($accessKey, $secretKey);
        $config = new Config();
        $bucketManager = new \Qiniu\Storage\BucketManager($auth, $config);

        $srcKey = $key;
        $fileName = basename($file);
        $key ='material/'.$id.'/'.$fileName;  //文件名
        $destKey = "move/".$key;
        $err = $bucketManager->move($bucket, $srcKey, $bucket, $destKey, true);
        print_r($err);exit;
    }



}